class BookmarkController < ApplicationController
  before_filter :login_required
  def index
    if(session[:user].nil?)
      redirect_to :controller=>"user",:action=>"login"
    else  
      userid=session[:user][:id]
      @results_bookmark=Bookmark.find(:all,:conditions=>["user_id=:id",{:id=>userid}])
    end
  end
  
  def add
    if(params[:id].nil?)then redirect_to :back 
      return
    end
    
    if Bookmark.find(:first,:conditions=>["user_id=:user_id and swarm_id=:swarm_id",{:user_id=>session[:user][:id],:swarm_id=>params[:id]}]).nil?  
      Bookmark.create(
                      :user_id=>session[:user][:id],
                      :swarm_id=>params[:id]
      )
      flash[:notice]="you have add it to your bookmark successly!"
      
    else
      flash[:notice]="you just can add the swarm once time."
    end
    redirect_to :action=>"index" 
    
    
    
  end
  
  def remove
    Bookmark.delete(params[:id])
    redirect_to :action=>"index"
  end
end
